﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Runtime.InteropServices;

namespace T.FAS.Runtime.Base.Entity
{
    [StructLayout(LayoutKind.Sequential)]
    /// <summary>
    /// 数据返回结果
    /// </summary>
    public sealed class TCommonResult<T> :TResult
    {
        public TCommonResult()
        {
        }
        public string ActionID { get; set; }
        /// <summary>
        /// Action Code
        /// </summary>
        public string ActionCode { get; set; }
        /// <summary>
        /// Action Name
        /// </summary>
        public string ActionName { get; set; }
        /// <summary>
        /// 数据结果返回类型 Record或者List
        /// </summary>
        public DataType ResultType { get; set; }
        /// <summary>
        /// DataType为Record和List时，实体类集合的Entity来源
        /// </summary>
        public TEntityType EntityType { get; set; }
        /// <summary>
        /// 当EntityType 为SGEO时，DataContent保存SG返回的数据
        /// </summary>
        public object DataContent { get; set; }
        /// <summary>
        /// Record数据结果
        /// </summary>
        public T Record { get; set; }
        private TArray<T> _list = null;
        /// <summary>
        /// List数据结果
        /// </summary>
        public TArray<T> List
        {
            get
            {
                if (this.Schema == null)
                    return null;
                if (_list == null && this.ResultType == DataType.List)
                {
                    _list = new TArray<T>(this.Schema.Entity);
                }
                return _list;
            }
            set
            {
                _list = value;
            }
        }
        /// <summary>
        /// 返回数据的Schema
        /// </summary>
        [JsonIgnore]
        public TSchema Schema { get; set; }
        public override object GetDataContent()
        {
            if (this.ResultType == DataType.Record)
                return this.Record;
            else
                return this.List;
        }
    }
}
